package com.atguigu.ssyx.activity.service.impl;

import com.atguigu.ssyx.activity.mapper.ActivityInfoMapper;
import com.atguigu.ssyx.activity.mapper.ActivityRuleMapper;
import com.atguigu.ssyx.activity.mapper.ActivitySkuMapper;
import com.atguigu.ssyx.activity.service.ActivityInfoService;
import com.atguigu.ssyx.client.product.ProductFeignClient;
import com.atguigu.ssyx.model.activity.ActivityInfo;
import com.atguigu.ssyx.model.activity.ActivityRule;
import com.atguigu.ssyx.model.activity.ActivitySku;
import com.atguigu.ssyx.model.product.SkuInfo;
import com.atguigu.ssyx.vo.activity.ActivityRuleVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 杨林
 * @create 2023-08-07 15:57 星期一
 * description:
 */
@Service
public class ActivityInfoServiceImpl extends ServiceImpl<ActivityInfoMapper, ActivityInfo> implements ActivityInfoService {

    @Autowired
    private ActivityInfoMapper activityInfoMapper;

    @Autowired
    private ActivityRuleMapper activityRuleMapper;
    
    @Autowired
    private ActivitySkuMapper activitySkuMapper;
    
    @Autowired
    private ProductFeignClient productFeignClient;

    /**
     * 获取活动分页列表
     * @param page
     * @param limit
     * @return
     */
    @Override
    public IPage<ActivityInfo> selectPage(Long page, Long limit) {
        Page<ActivityInfo> activityInfoPage = new Page<>(page, limit);
        Page<ActivityInfo> pageModel = activityInfoMapper.selectPage(activityInfoPage, null);
        //分页查询对象里面获取到列表数据
        List<ActivityInfo> activityInfoList = activityInfoPage.getRecords();
        //遍历activityInfoList，向每个ActivityInfo对象中封装activityTypeString属性
        activityInfoList.stream().forEach(item -> {
            item.setActivityTypeString(item.getActivityType().getComment());
        });
        return pageModel;
    }


    /**
     * 根据活动id获取活动规则
     * @param id
     * @return
     */
    @Override
    public Map<String,Object> findActivityRuleList(Long id) {
        Map<String,Object> result = new HashMap<>();
        
        //1、获取活动规则
        LambdaQueryWrapper<ActivityRule> wrapper1 = new LambdaQueryWrapper<>();
        wrapper1.eq(ActivityRule::getActivityId,id);
        List<ActivityRule> activityRuleList = activityRuleMapper.selectList(wrapper1);
        result.put("activityRuleList",activityRuleList);
        
        //2、获取参与活动的商品
        LambdaQueryWrapper<ActivitySku> wrapper2 = new LambdaQueryWrapper<>();
        wrapper2.eq(ActivitySku::getActivityId,id);
        List<ActivitySku> activitySkuList = activitySkuMapper.selectList(wrapper2);
        
        List<SkuInfo> skuInfoList = new ArrayList<>();
        if (activitySkuList != null && activitySkuList.size() != 0){
            //收集skuId
            List<Long> skuIdList = activitySkuList.stream().map(ActivitySku::getSkuId).collect(Collectors.toList());
            skuInfoList = productFeignClient.findSkuInfoList(skuIdList);
        }
        result.put("skuInfoList",skuInfoList);
        return result;
    }


    /**
     * 新增活动规则
     * @param activityRuleVo
     */
    @Override
    @Transactional   //整体是一个事务
    public void saveActivityRule(ActivityRuleVo activityRuleVo) {
        //1、删除原有的活动规则
        activityRuleMapper.delete(new LambdaQueryWrapper<ActivityRule>()
                .eq(ActivityRule::getActivityId,activityRuleVo.getActivityId()));
        //2、删除活动规则于商品关联表中的数据（activity_sku）
        activitySkuMapper.delete(new LambdaQueryWrapper<ActivitySku>()
                .eq(ActivitySku::getActivityId,activityRuleVo.getActivityId()));
        
        //3、获取活动规则list
        List<ActivityRule> activityRuleList = activityRuleVo.getActivityRuleList();
        //4、获取活动参与商品list
        List<ActivitySku> activitySkuList = activityRuleVo.getActivitySkuList();
        //5、获取优惠券id列表
        List<Long> couponIdList = activityRuleVo.getCouponIdList();
        
        //6、根据活动id获取活动的详细信息
        ActivityInfo activityInfo = activityInfoMapper.selectById(activityRuleVo.getActivityId());
        activityRuleList.forEach(item -> {
            item.setActivityId(activityInfo.getId());
            item.setActivityType(activityInfo.getActivityType());
        });
        //7、批量保存
        activityRuleMapper.batchInsert(activityRuleList);
    
        //8、保存ActivitySku
        activitySkuList.forEach(item -> {
            item.setActivityId(activityRuleVo.getActivityId());
        });
        //9、批量保存
        activitySkuMapper.batchInsert(activitySkuList);
    }


    /**
     * 根据关键字获取skuInfo列表，提供给添加活动范围使用
     * @param keyword
     * @return
     */
    @Override
    public List<SkuInfo> findSkuInfoByKeyword(String keyword) {
        List<SkuInfo> skuInfoList = productFeignClient.findSkuInfoByKeyWord(keyword);
        return skuInfoList;
    }

}
